//
//  CheckOutManager.m
//  zhima_kt
//
//  Created by zhima on 2020/3/6.
//  Copyright © 2020 zhima. All rights reserved.
//

#import "CheckOutManager.h"

@implementation CheckOutManager

SINGLETON_FOR_CLASS(CheckOutManager)

-(BOOL)isBlankString:(NSString *)str{
    if(str==nil || str==NULL){
            return YES;
        }
        if([str isKindOfClass:[NSNull class]]){
            return YES;
        }
        if([str isEqual:[NSNull null]]) {
            return YES;
        }
        if([str isEqualToString:[NSString stringWithFormat:@"%@",nil]]){
            return YES;
        }
        if([str isEqualToString:[NSString stringWithFormat:@"%@",[NSNull null]]]){
            return YES;
        }
        
        if([[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0){
            return  YES;
        }
        return  NO;
}

-(BOOL)isBlankArray:(NSArray *)array{
    if (array!=nil && ![array isKindOfClass:[NSNull class]] && [array count]!=0) {
        return NO;
    } else {
        return YES;
    }
}
-(BOOL)isBlankDictionary:(NSDictionary*)dict{
    if (dict!=nil && ![dict isKindOfClass:[NSNull class]] && [dict count]!=0) {
        return NO;
    } else {
        return YES;
    }
}

-(BOOL)isValidateEmail:(NSString *)candidate{
    NSArray *array = [candidate componentsSeparatedByString:@"."];
        if ([array count] >= 4) {
            return NO;
        }
        NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
        NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
        return [emailTest evaluateWithObject:candidate];
}

-(BOOL)isValidNumber:(NSString *)value{
    const char *cvalue = [value UTF8String];
        int len = strlen(cvalue);
        for (int i = 0; i < len; i++) {
            if(!isNumber(cvalue[i])){
                return NO;
            }
        }
        return YES;
  
}


BOOL isNumber (char ch){
    if (!(ch >= '0' && ch <= '9')) {
        return FALSE;
    }
    return TRUE;
}

    

//因为各个运营商开头号码不同，这里只匹配11位是比较好的方法
- (BOOL)isValidPhoneNumber:(NSString *)phone {
    NSString *regex = @"^[0-9]{11}";
    return [self isValidateByRegex:regex Str:phone];
}



/// 正则基础封装
- (BOOL)isValidateByRegex:(NSString *)regex Str:(NSString *)value {
    NSPredicate *pre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [pre evaluateWithObject:value];
}

@end
